<?php
class AdminHelper extends AppHelper{
    function admin_url()
    {
        return Router::url('/', true).'admin/';
    }
    
    function edit_url_image($url = null)
    {
        $arr_url_img = explode('/', $url);
        $name_img = end($arr_url_img);
        $i = 0;
        if($name_img != 'no-image.jpg')
        {
            $url_folder = "";
            foreach($arr_url_img as $key => $value)
            {
                if($i == 1 && $value != $name_img)
                {
                    $url_folder .= $value.'/';
                }
                if($value == 'upload')
                    $i = 1;
            }
            return $url_folder . $name_img;
        }
        else
        {
            return "";
        }
    }
    
    function file_upload($data = array())
    {
        $html = "";
        $html.='<span class="btn btn-success fileinput-button">';
            $html.='<i class="glyphicon glyphicon-plus"></i>';
            $html.='<span>Select files...</span>';
            $html.='<input id="fileupload" type="file" name="files[]" multiple>';
        $html.='</span>';
        $html.='<br/><br/>';
        $html.='<div id="progress" class="progress">';
            $html.='<div class="progress-bar progress-bar-success"></div>';
        $html.='</div>';
        $html.='<div id="files" class="files"></div><br/>';


        $data = json_encode($data);
        ?>
            <script>
                var dataString = <?php echo $data ?>;
                var jsonString = JSON.stringify(dataString);
            </script>
        <?php
        $html.="
            <script>
                $(document).ready(function() {
                    var url = '/demo/server/php/';
                    $('#fileupload').fileupload({
                        formData: {data: jsonString},
                        url: url,
                        dataType: 'json',
                        done: function (e, data) {
                            $.each(data.result.files, function (index, file) {
                                $('<p/>').text(file.name).appendTo('#files');
                            });
                        },
                        progressall: function (e, data) {
                            var progress = parseInt(data.loaded / data.total * 100, 10);
                            $('#progress .progress-bar').css(
                                'width',
                                progress + '%'
                            );
                        }
                    }).prop('disabled', !$.support.fileInput)
                        .parent().addClass($.support.fileInput ? undefined : 'disabled');
                });
            </script>
        ";
        return $html;
    }
    
    function file_upload_bk()
    {
        $html = "";
        $html.='<span class="btn btn-success fileinput-button">';
            $html.='<i class="glyphicon glyphicon-plus"></i>';
            $html.='<span>Select files...</span>';
            $html.='<input id="fileupload" type="file" name="files[]" multiple>';
        $html.='</span>';
        $html.='<br/><br/>';
        $html.='<div id="progress" class="progress">';
            $html.='<div class="progress-bar progress-bar-success"></div>';
        $html.='</div>';
        $html.='<div id="files" class="files"></div><br/>';
        return $html;
    }
    
    function textarea($name = null)
    {
        $html = "";
        $html.='<textarea class="form-control" id="'.$name.'" name="'.$name.'"></textarea>';
        $html.='<script>';
            $html.='var '.$name.' = CKEDITOR.replace("'.$name.'");';
            $html.='CKFinder.setupCKEditor( '.$name.', "'.$this->webroot .'ckfinder/'.'");';
        $html.='</script>';
        return $html;
    }
    
    function unicode_convert($str){
    	if(!$str) return false;
    	$unicode = array(
    				'a'=>array('á','à','ả','ã','ạ','ă','ắ','ặ','ằ','ẳ','ẵ','â','ấ','ầ','ẩ','ẫ','ậ'),
    				'A'=>array('Á','À','Ả','Ã','Ạ','Ă','Ắ','Ặ','Ằ','Ẳ','Ẵ','Â','Ấ','Ầ','Ẩ','Ẫ','Ậ'),
    				'd'=>array('đ'),
    				'D'=>array('Đ'),
    				'e'=>array('é','è','ẻ','ẽ','ẹ','ê','ế','ề','ể','ễ','ệ'),
    				'E'=>array('É','È','Ẻ','Ẽ','Ẹ','Ê','Ế','Ề','Ể','Ễ','Ệ'),
    				'i'=>array('í','ì','ỉ','ĩ','ị'),
    				'I'=>array('Í','Ì','Ỉ','Ĩ','Ị'),
    				'o'=>array('ó','ò','ỏ','õ','ọ','ô','ố','ồ','ổ','ỗ','ộ','õ','ớ','ờ','ở','ỡ','ợ'),
    				'O'=>array('Ó','Ò','Ỏ','Õ','Ọ','Ô','Ố','Ồ','Ổ','Ỗ','Ộ','Õ','Ớ','Ờ','Ở','Ỡ','Ợ'),
    				'u'=>array('ú','ù','ủ','ũ','ụ','ý','ứ','ừ','ử','ữ','ự'),
    				'U'=>array('Ú','Ù','Ủ','Ũ','Ụ','Ý','Ứ','Ừ','Ử','Ữ','Ự'),
    				'y'=>array('ý','ỳ','ỷ','ỹ','ỵ'),
    				'Y'=>array('Ý','Ỳ','Ỷ','Ỹ','Ỵ'),
    				'-'=>array(' ','.','/',"'",'`','(',')',',','!','"','"','"','%','&','@','#','$'),
    				);
    	foreach($unicode as $nonUnicode=>$uni){
    		foreach($uni as $value)
    		$str = str_replace($value,$nonUnicode,$str);
    	}
        $str = Inflector::slug($str, '-');
    	$str = strtolower($str);
    	return $str;
    }
    
}